using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Mbs.ComponentModel.Container.Context;
using Mbs.ComponentModel.Container.Metadata;

namespace Mbs.ComponentModel.Container.Injection
{
    public abstract class InspectAdapter<TMember> : MarshalAndDisposableObject, IInspect<TMember> where TMember : MemberInfo
    {
        protected IKernel Kernel { get; set; }
        protected IComponentInfo Component { get; set; }

        IInjectContext ICommand<IInspectContext, IInjectContext>.Execute(IInspectContext ctx)
        {
            return Execute(ctx);
        }


        public virtual IInjectContext<TMember> Execute(IInspectContext ctx)
        {
            if (ctx == null
                || ctx.Kernel == null
                || ctx.Instance == null)
                return null;

            Kernel = ctx.Kernel as IKernel;
            Component = ctx.Component;
            return DoExecute(ctx);
        }

        protected abstract IInjectContext<TMember> DoExecute(IInspectContext ctx);


        #region IInspect<TMember> Members

        public System.Collections.Generic.KeyValuePair<TMember, string>[] Members{get;protected set;}

        #endregion

        #region IInspect Members

        System.Collections.Generic.KeyValuePair<MemberInfo, string>[] IInspect.Members
        {
            get 
            {
                return (from item in Members 
                        select new KeyValuePair<MemberInfo,string>(item.Key,item.Value)).ToArray();
            }
        }

        #endregion
    }
}
